package com.liannong.controller;

import com.liannong.common.Result;
import com.liannong.service.BlockchainStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区块链存储管理控制器
 * 提供区块链存储管理相关的API接口
 */
@Slf4j
@RestController
@RequestMapping("/api/blockchain")
public class BlockchainController {

    @Autowired
    private BlockchainStorageService blockchainStorageService;
    
    /**
     * 获取所有区块分组信息
     */
    @GetMapping("/groups")
    public Result getBlockGroups() {
        try {
            List<Map<String, Object>> groups = blockchainStorageService.getAllBlockGroups();
            Map<String, Object> data = new HashMap<>();
            data.put("groups", groups);
            return Result.success(data);
        } catch (Exception e) {
            log.error("获取区块分组信息失败", e);
            return Result.error("500", "获取区块分组信息失败");
        }
    }
    
    /**
     * 获取当前阈值设置
     */
    @GetMapping("/thresholds")
    public Result getThresholds() {
        try {
            Map<String, Integer> thresholds = blockchainStorageService.getThresholds();
            return Result.success(thresholds);
        } catch (Exception e) {
            log.error("获取阈值设置失败", e);
            return Result.error("500", "获取阈值设置失败");
        }
    }
    
    /**
     * 更新阈值设置
     */
    @PostMapping("/updateThresholds")
    public Result updateThresholds(@RequestBody Map<String, Integer> thresholds) {
        try {
            // 验证参数
            if (!thresholds.containsKey("blockCount") || !thresholds.containsKey("transactionCount")) {
                return Result.error("400", "参数错误");
            }
            
            int blockCount = thresholds.get("blockCount");
            int transactionCount = thresholds.get("transactionCount");
            
            // 验证阈值范围
            if (blockCount < 100 || blockCount > 10000 || 
                transactionCount < 500 || transactionCount > 20000) {
                return Result.error("400", "阈值超出有效范围");
            }
            
            blockchainStorageService.updateThresholds(blockCount, transactionCount);
            return Result.success("阈值更新成功");
        } catch (Exception e) {
            log.error("更新阈值设置失败", e);
            return Result.error("500", "更新阈值设置失败");
        }
    }
    
    /**
     * 手动切换区块分组
     */
    @PostMapping("/switchGroup")
    public Result switchGroup() {
        try {
            String newGroupId = blockchainStorageService.manualSwitchBlockGroup();
            Map<String, String> data = new HashMap<>();
            data.put("newGroupId", newGroupId);
            return Result.success(data);
        } catch (Exception e) {
            log.error("切换区块分组失败", e);
            return Result.error("500", "切换区块分组失败");
        }
    }
    
    /**
     * 激活指定区块分组
     */
    @PostMapping("/activateGroup/{groupId}")
    public Result activateGroup(@PathVariable String groupId) {
        try {
            boolean success = blockchainStorageService.activateBlockGroup(groupId);
            if (success) {
                return Result.success("激活区块分组成功");
            } else {
                return Result.error("404", "区块分组不存在或无法激活");
            }
        } catch (Exception e) {
            log.error("激活区块分组失败", e);
            return Result.error("500", "激活区块分组失败");
        }
    }
    
    /**
     * 删除指定区块分组
     */
    @DeleteMapping("/group/{groupId}")
    public Result deleteGroup(@PathVariable String groupId) {
        try {
            boolean success = blockchainStorageService.deleteBlockGroup(groupId);
            if (success) {
                return Result.success("删除区块分组成功");
            } else {
                return Result.error("400", "无法删除当前活跃分组或分组不存在");
            }
        } catch (Exception e) {
            log.error("删除区块分组失败", e);
            return Result.error("500", "删除区块分组失败");
        }
    }
    
    /**
     * 获取区块分组的状态统计信息
     */
    @GetMapping("/groupStats/{groupId}")
    public Result getGroupStats(@PathVariable String groupId) {
        try {
            Map<String, Object> stats = blockchainStorageService.getBlockGroupStats(groupId);
            if (stats == null) {
                return Result.error("404", "区块分组不存在");
            }
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取区块分组统计信息失败", e);
            return Result.error("500", "获取区块分组统计信息失败");
        }
    }
} 